package ca.uwindsor;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;

public class AddSemester extends Activity {

	String sem;
	String year;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_semester);

		final MyDBAdapter db = new MyDBAdapter(this);
		db.setDATABASE_TABLE("semester");

		/*String DATABASE_CREATE = "create table " + db.getDATABASE_TABLE()
				+ " (" + "_id" + " integer primary key autoincrement, "
				+ "Name" + " text not null, " + "StartDate"
				+ " text not null, " + "EndDate" + " text not null, "
				+ "Comments" + " text" + ");";
		db.setDATABASE_CREATE(DATABASE_CREATE);*/

		db.open();

		// spinner term
		Spinner term = (Spinner) findViewById(R.id.term);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.term_sem, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		term.setAdapter(adapter);
		term.setOnItemSelectedListener(new MyOnItemSelectedListener()); // class
																		// below

		Calendar cal = Calendar.getInstance(); // to get current year
		int y = cal.get(Calendar.YEAR);

		// spinner year
		Spinner sYear = (Spinner) findViewById(R.id.year);
		ArrayAdapter<CharSequence> yearAd = new ArrayAdapter<CharSequence>(
				this, android.R.layout.simple_spinner_item);
		yearAd.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		for (int i = 0; i < 3; i++) {
			yearAd.add(Integer.toString(y)); // add to spinner year
			y = y + 1;
		}
		sYear.setAdapter(yearAd);
		sYear.setOnItemSelectedListener(new MyOnItemSelectedListener());

		// Alert Dialog builder
		final AlertDialog.Builder alert = new AlertDialog.Builder(this);

		Button add_but = (Button) findViewById(R.id.add_button);
		add_but.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// SQL code
				final MyDBAdapter db = new MyDBAdapter(getApplicationContext());
				db.setDATABASE_TABLE("semester");
				db.open();
				ContentValues newValues = new ContentValues();

				newValues.put("Name", sem + " " + year);

				DatePicker startDate = (DatePicker) findViewById(R.id.dateStart);
				String startDateStr = startDate.getYear() + "-"
						+ (startDate.getMonth()+1) + "-"
						+ startDate.getDayOfMonth();
				newValues.put("StartDate", startDateStr);

				DatePicker endDate = (DatePicker) findViewById(R.id.dateEnd);
				String endDateStr = endDate.getYear() + "-"
						+ (endDate.getMonth()+1) + "-" + endDate.getDayOfMonth();
				newValues.put("EndDate", endDateStr);

				EditText comments = (EditText) findViewById(R.id.txtComments);
				newValues.put("Comments", comments.getText().toString());
				
				Common c = new Common();
				if(c.CompareDates(endDateStr, startDateStr)<=0)
				{
					final AlertDialog.Builder alert = new AlertDialog.Builder((AddSemester.this));
					alert.setMessage("End date should be greater than start date");
					alert.setNegativeButton("OK", new OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
						}

					});
					alert.show();
				}
				else{
				// Check if sem name already exists.
				Cursor ret = db.getEntry("Name", sem + " " + year);
				if (ret.getCount() != 0) {
					// Alert the user
					alert.setMessage("Term already present in the database.");

					alert.setNegativeButton("Go Back", new OnClickListener() {

						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							// do nothing
						}

					});
					alert.show();
				} else {
					// insert
					db.insertEntry(newValues);
					db.close();

					alert.setMessage("Semester added.");

					alert.setNegativeButton("OK", new OnClickListener() {

						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							startActivity(new Intent("ca.uwindsor.WELCOMEMENU"));
						}

					});
					alert.setCancelable(false);
					alert.show();
				}
				
			}
				db.close();
			}
		});
		db.close();
	}

	public class MyOnItemSelectedListener implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {
			// System.out.print(parent.getAdapter().toString());
			switch (parent.getId()) {
			case R.id.term:
				sem = parent.getItemAtPosition(pos).toString();
				break;
			case R.id.year:
				year = parent.getItemAtPosition(pos).toString();
				break;

			}

		}

		public void onNothingSelected(AdapterView<?> parent) {
			// Do nothing.
		}
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		finish();
	}

}
